let App = getApp();

Page({
  data: {
    code: '',
    username: '',
    password: '',
    verCode: '',
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    //刚进入页面随机先获取一个
    console.info("registRecommendId",App.globalData.recommendId);
    this.createCode()

  },

  getPhone: function(e) {
    let _this = this;
    _this.data.username = e.detail.value;


  },
  getPwd: function(e) {
    let _this = this;
    _this.data.password = e.detail.value;

  },
  getCode: function(e) {
    let _this = this;
    _this.data.verCode = e.detail.value;
  },

  huanyizhang() {
    this.createCode()
  },
  createCode() {
    var code;
    //首先默认code为空字符串
    code = '';
    //设置长度，这里看需求，我这里设置了4
    var codeLength = 4;
    //设置随机字符
    var random = new Array(0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z');
    //循环codeLength 我设置的4就是循环4次
    for (var i = 0; i < codeLength; i++) {
      //设置随机数范围,这设置为0 ~ 36
      var index = Math.floor(Math.random() * 36);
      //字符串拼接 将每次随机的字符 进行拼接
      code += random[index];
    }
    //将拼接好的字符串赋值给展示的code
    this.setData({
      code: code
    })
  },
  doRegist: function(e) {
    var _this = this;
    var verCode = _this.data.verCode;
    var username = _this.data.username;
    var password = _this.data.password;

    // 简单验证
    if (username.length == 0 || password.length == 0) {
      wx.showToast({
        title: '用户名或密码不能为空',
        icon: 'none',
        duration: 3000
      })
    } else if (_this.data.code.toLowerCase() != verCode.toLowerCase()) {
      wx.showToast({
        title: '验证码不正确',
        icon: 'none',
        duration: 3000
      });


    } else {
      wx.showLoading({
        title: "正在注册",
        mask: true
      });
      // 执行微信登录
      wx.login({
        success: function(res) {
          // 发送用户信息
          App._post_form('user/regist', {
            username: username,
            password: password,
            recommendId:App.globalData.recommendId,
            code: res.code,
            user_info: e.detail.rawData,
            encrypted_data: e.detail.encryptedData,
            iv: e.detail.iv,
            signature: e.detail.signature
          }, function(result) {
            // 记录token user_id
          //  console.info("success", result.data.session_key);
           // console.info("result.data.user_id", result.data.user_id);
            //wx.setStorageSync('token', result.data.session_key);
            //wx.setStorageSync('user_id', result.data.user_id);
            //wx.setStorageSync('role', result.data.role);
            if(result.status == 200){
          wx.showModal({
              title: '提示',
              content: '注册成功，请登录',
              showCancel: false,
              success: function(res) {
                wx.navigateTo({
                  url: '/pages/login/userLogin',
                })
              }


            });
            }
          }, false, function() {
          
            wx.showModal({
              title: '提示',
              content: '该微信号已绑定用户，请登录',
              showCancel: false,
              success: function (res) {
                wx.navigateTo({
                  url: '/pages/login/userLogin',
                })
              }


            });
      
            
            wx.hideLoading();
          });
        }
      });
    }
  },
  goWxLogin: function () {
    wx.redirectTo({
      url: '../login/login',
    })
  },
  goLoginPage: function() {
    wx.navigateTo({
      url: '../login/userLogin',
    })
  },
})